package com.example.treehole.ui.page.postDetail

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.treehole.LocalPostDetailViewModel
import com.example.treehole.LocalUserViewModel
import com.example.treehole.ui.model.CommentType
import com.example.treehole.ui.theme.Cyan
import com.example.treehole.ui.theme.backCyan
import kotlinx.coroutines.launch

@Composable
fun InputCommentCard(focusRequester: FocusRequester, postId: Int) {
    val createCommentViewModel = LocalCreateCommentViewModel.current
    val userComment by createCommentViewModel.userComment.collectAsState()
    val replyUserName by createCommentViewModel.replyUserName.collectAsState()

    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()
    val scope = rememberCoroutineScope()

    val postDetailViewModel = LocalPostDetailViewModel.current

    Box(
        modifier =
        Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp))
            .background(Cyan)
    ) {
        Column(
            modifier =
            Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp, horizontal = 12.dp)
                .background(Color.White, RoundedCornerShape(16.dp))
                .wrapContentSize()
        ) {
            if (replyUserName.isNotEmpty()) {
                Text(
                    text = "回复 $replyUserName",
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.Gray,
                    modifier = Modifier.padding(start = 12.dp, top = 12.dp)
                )
            }
            BasicTextField(
                value = userComment,
                onValueChange = { createCommentViewModel.updateUserComment(it) },
                textStyle = MaterialTheme.typography.bodyMedium,
                modifier = Modifier
                    .fillMaxWidth()
                    .focusRequester(focusRequester),
                decorationBox = { innerTextField ->
                    Column {
                        Box(
                            modifier =
                            Modifier
                                .fillMaxWidth()
                                .padding(12.dp)
                                .heightIn(min = 0.dp, max = 300.dp)
                        ) {
                            if (userComment.isEmpty()) {
                                Text(
                                    text = "请输入评论",
                                    style = MaterialTheme.typography.bodyMedium,
                                    color = Color.Gray
                                )
                            }
                            innerTextField()
                        }
                        Row(
                            modifier =
                            Modifier
                                .fillMaxWidth()
                                .padding(end = 12.dp, bottom = 8.dp),
                            horizontalArrangement = Arrangement.End
                        ) {
                            Button(
                                onClick = {
                                    scope.launch {
                                        userId?.let {
                                            val res = createCommentViewModel.uploadComment(jwlToken,it,postId)
                                            res?.let { it1 ->
                                                postDetailViewModel.addNewComment(it1)
                                                createCommentViewModel.updateCommentingState(false)
                                                createCommentViewModel.updateUserComment("")
                                                createCommentViewModel.updateReplyUserName("")
                                                createCommentViewModel.updateCmtType(CommentType.CommentToPost)
                                                postDetailViewModel.updateCommenting(true)
                                            }
                                        }
                                    }
                                },
                                colors =
                                ButtonDefaults.buttonColors(
                                    containerColor = backCyan
                                ),
                                shape = RoundedCornerShape(12.dp)
                            ) { Text("发布") }
                        }
                    }
                }
            )
        }
    }
}
